#include "LCommon.h"

static bool running = true;
Database* db_logon;
ConfigFile* logon_config;

void OnSig(int s)
{
	running = false;
}

void _HookSignals()
{
	signal(SIGINT, OnSig);
	signal(SIGTERM, OnSig);
	signal(SIGABRT, OnSig);
	signal(SIGBREAK, OnSig);
	Log->Notice("Master", "Hooking signals...");
}


int main()
{
	new CLog();
	new FileLogHandler;
	FileLog.AddLogFile(LOG_OBJECT, "ObjectLog.txt");
	logon_config = new ConfigFile("logonserver.txt");
	if(!logon_config->Load())
	{
		getchar();
		return 0;
	}
	db_logon = new Database();
	LogonDb.Connect(logon_config->GetChar("LogonDatabase", "Host"), logon_config->GetChar("LogonDatabase", "User"), logon_config->GetChar("LogonDatabase", "Pass"), 
		logon_config->GetChar("LogonDatabase", "Database"), 3306);

	ClientListener* listen = new ClientListener();
	new AccountManager();
	AccountWorker* aw = new AccountWorker();
	new RealmHandler();
	ThreadPool.Start();
	ThreadPool.ExecuteTask(sRealmHandler);
	ThreadPool.ExecuteTask(listen);
	ThreadPool.ExecuteTask(aw);

	_HookSignals();
	DWORD timer = GetTickCount();
	while(running)
	{
		Sleep(50);
		if(GetTickCount() - timer > 10000)
		{
			timer = GetTickCount();
			//ThreadPool.SanityCheck();
		}
	}
	Log->Notice("Master", "Shuting down... Konsole bitte nicht schliessen!");
	ThreadPool.ShutDown();
}